package win.zxshuo.www.controller.responses;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import win.zxshuo.www.entity.CodeConstant;
import win.zxshuo.www.entity.MsgConstant;
import win.zxshuo.www.exception.BaseException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseResponse {
    private Boolean status;

    private Integer code;

    private Object data;

    public void setTrue(){
        this.status = true;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public BaseResponse setErrorMsg(String errMsg){
        Map<String,String> map = new HashMap<>();
        map.put("err_msg",errMsg);
        this.setStatus(false);
        this.setData(map);
        return this;
    }

    public BaseResponse setErrorMsg(Object data){
        this.setData(data);
        this.setStatus(false);
        return this;
    }

    public BaseResponse setErrBindingResult(BindingResult result){
        Map<String,List<String>> errMsgs = new HashMap<>();

        List<String> errs = new ArrayList<>();

        List<ObjectError> errorList = result.getAllErrors();
        for (ObjectError objectError:errorList){
            errs.add(objectError.getDefaultMessage());
        }
        errMsgs.put("err_msg",errs);
        this.setData(errMsgs);
        this.setStatus(false);
        return this;
    }

    public void setTrueData(Object data){
        this.setData(data);
        this.setTrue();
    }

    public void setFalse(){
        this.status = false;
    }

    public static BaseResponse getFailedResponse(String errMsg){
        BaseResponse baseResponse= new BaseResponse();
        baseResponse.status = false;
        baseResponse.code = -1;
        Map<String,String> err = new HashMap<>();
        err.put("err_msg",errMsg);
        baseResponse.setData(err);
        return baseResponse;
    }

    /**
     * 通过exception来获取错误返回
     * @param exception
     * @return
     */
    public static BaseResponse getFailedResponseWithBaseException(BaseException exception){
        BaseResponse baseResponse= new BaseResponse();
        baseResponse.setFalse();
        Map<String,String> errMsg = new HashMap<>();
        if (exception.getMessage() == null){
            errMsg.put("err_msg",MsgConstant.GENERAL_FAILED);
        }else {
            errMsg.put("err_msg",exception.getMessage());
        }
        baseResponse.setData(errMsg);
        if (exception.getCode() == null){
            baseResponse.setCode(-1);
        }else {
            baseResponse.setCode(exception.getCode());
        }
        return baseResponse;
    }

    public static BaseResponse getFailedResponseWithException(Exception exception){
        BaseResponse baseResponse= new BaseResponse();
        baseResponse.setFalse();
        Map<String,String> errMsg = new HashMap<>();
        if (exception.getMessage() == null){
            errMsg.put("err_msg",MsgConstant.GENERAL_FAILED);
        }else {
            errMsg.put("err_msg",exception.getMessage());
        }
        baseResponse.setData(errMsg);
        baseResponse.setCode(CodeConstant.GENERAL_FAILED);
        return baseResponse;
    }


}
